import request from '@/utils/request'

export const projectTablePageConfigPostRequire = (server, params) => {
    if (params.searchFieldsArr) {
        params.searchFields = params.searchFieldsArr.toString()
    }
    if (params.tableColumnFieldsArr) {
        params.tableColumnFields = params.tableColumnFieldsArr.toString()
    }
    if (params.addFieldsArr) {
        params.addFields = params.addFieldsArr.toString()
    }
    if (params.updateFieldsArr) {
        params.updateFields = params.updateFieldsArr.toString()
    }
    params.projectDatasource = undefined
    return new Promise((resolve) => {
        request({
            url: 'api/projectTablePageConfig/' + server,
            method: 'post',
            data: params
        }).then((response) => {
            if ("listPage" === server && response.data && response.data.records) {
                let arr = response.data.records || []
                arr.forEach(a => {
                    if (a.tableColumnFields && a.tableColumnFields.length > 0) {
                        a.tableColumnFieldsArr = a.tableColumnFields.split(",")
                    }
                    if (a.searchFields && a.searchFields.length > 0) {
                        a.searchFieldsArr = a.searchFields.split(",")
                    }
                    if (a.addFields && a.addFields.length > 0) {
                        a.addFieldsArr = a.addFields.split(",")
                    }
                    if (a.updateFields && a.updateFields.length > 0) {
                        a.updateFieldsArr = a.updateFields.split(",")
                    }
                })
            }
            resolve(response)
        })
    })
}

export const projectTablePageConfigPostRequireByForm = (server, params) => {
    if (params.searchFieldsArr) {
        params.searchFields = params.searchFieldsArr.toString()
    }
    if (params.tableColumnFieldsArr) {
        params.tableColumnFields = params.tableColumnFieldsArr.toString()
    }
    if (params.addFieldsArr) {
        params.addFields = params.addFieldsArr.toString()
    }
    if (params.updateFieldsArr) {
        params.updateFields = params.updateFieldsArr.toString()
    }
    params.projectDatasource = undefined
    return request({
        url: 'api/projectTablePageConfig/' + server,
        method: 'post',
        data: params,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
}
